package com.secneo.appshare.activities;

import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;

import com.secneo.appshare.R;
import com.secneo.appshare.common.ActivityUtil;
import com.secneo.appshare.common.AppSharePreference;
import com.secneo.appshare.common.Constants;
import com.secneo.mp.api.database.DatabaseHelper;

public class CommonMenuActivity extends Activity
{
	public static final int HELP = Menu.FIRST;
	public static final int INEDX = Menu.FIRST + 1;
	public static final int LOGINOUT = Menu.FIRST + 4;
	public static final int MEMBER_SETTING = Menu.FIRST + 5;
	public static final int MEMBER_SYNCHRONIZATION = Menu.FIRST + 6;
	public static final int MEMBER_FIRENDS = Menu.FIRST + 7;
	Intent intent = new Intent();

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		String help = this.getResources().getString(R.string.menu_help);
		String index = this.getResources().getString(R.string.menu_index);
		String loginout = this.getResources().getString(R.string.menu_logout);
		String memberSetting = this.getResources().getString(R.string.menu_member_setting);
		String synchronization = this.getResources().getString(R.string.menu_synchronization);
		String firends = this.getResources().getString(R.string.menu_firends);

		menu.add(0, INEDX, 1, index).setIcon(R.drawable.menu_index);
		menu.add(0, MEMBER_SYNCHRONIZATION, 2, synchronization).setIcon(R.drawable.menu_synchronization);
		menu.add(0, MEMBER_SETTING, 3, memberSetting).setIcon(R.drawable.menu_setting);
		menu.add(0, HELP, 4, help).setIcon(R.drawable.menu_help);
		menu.add(0, MEMBER_FIRENDS, 5, firends).setIcon(R.drawable.menu_firends);
		menu.add(0, LOGINOUT, 6, loginout).setIcon(R.drawable.menu_loginout);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
			case HELP:
				HelpWebViewActivity2.go(this, Constants.URL_HELP, getString(R.string.menu_help));
				return true;
				
			case INEDX:
				intent.setClass(this, MainActivity.class);
				intent.putExtra("weiboLogin", false);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				intent.putExtra(Constants.MAINACTIVITYINDEXACTION, 0);
				startActivity(intent);
				finish();
				return true;

			case LOGINOUT:
				ActivityUtil.closeSelfApp(this);
				return true;
				
			case MEMBER_SETTING:
				if (!AppSharePreference.isAppShareUserLogined(CommonMenuActivity.this))
				{
					intent.setClass(this, LoginChooseMethodActivity.class);
					startActivity(intent);
				} else
				{
					intent.setClass(this, MemberSettingActivity.class);
					startActivity(intent);
				}
				return true;

			case MEMBER_SYNCHRONIZATION:
				DatabaseHelper dh = new DatabaseHelper(CommonMenuActivity.this);
				dh.insertUserAppPopularityBycatagory(CommonMenuActivity.this, 2029);
				dh.close();
				if (!AppSharePreference.isAppShareUserLogined(CommonMenuActivity.this))
				{
					intent.setClass(this, LoginChooseMethodActivity.class);
					startActivity(intent);
				} else
				{
					intent.setClass(this, AccountManagementActivity.class);
					startActivity(intent);
				}
				return true;

			case MEMBER_FIRENDS:
				DatabaseHelper dh1 = new DatabaseHelper(CommonMenuActivity.this);
				dh1.insertUserAppPopularityBycatagory(CommonMenuActivity.this, 2030);
				dh1.close();
				ActivityUtil.userShare(this, this.getResources().getString(R.string.share_content), 2);
				return true;
				
			default:
				break;
		}
		return super.onOptionsItemSelected(item);
	}
}
